class PWAManager {
    constructor() {
        this.initializeApp();
    }

    initializeApp() {
        // 添加安装提示
        let deferredPrompt;
        window.addEventListener('beforeinstallprompt', (e) => {
            e.preventDefault();
            deferredPrompt = e;
            this.showInstallPrompt(deferredPrompt);
        });

        // 检测网络状态
        this.checkOnlineStatus();
    }

    // PWA应用安装提示UI
    showInstallPrompt(deferredPrompt) {
        const installButton = document.createElement('button');
        installButton.textContent = '安装应用';
        installButton.classList.add('install-button');
        document.body.appendChild(installButton);

        installButton.addEventListener('click', async () => {
            deferredPrompt.prompt();
            const { outcome } = await deferredPrompt.userChoice;
            if (outcome === 'accepted') {
                console.log('用户接受安装');
            }
            installButton.remove();
        });
    }

    // 注册更新监听事件
    checkOnlineStatus() {
        window.addEventListener('online', this.updateOnlineStatus);
        window.addEventListener('offline', this.updateOnlineStatus);
    }

    // 控制提示显示与隐藏
    updateOnlineStatus() {
        const offlineMessage = document.getElementById('offline-message');
        if (navigator.onLine) {
            offlineMessage.style.display = 'none';
        } else {
            offlineMessage.style.display = 'block';
        }
    }
}

export default PWAManager;
